// File generated from our OpenAPI spec by Stainless.

package com.metest.api.services.async.rest

import com.metest.api.core.ClientOptions
import com.metest.api.core.RequestOptions
import com.metest.api.core.handlers.errorBodyHandler
import com.metest.api.core.handlers.errorHandler
import com.metest.api.core.handlers.jsonHandler
import com.metest.api.core.http.HttpMethod
import com.metest.api.core.http.HttpRequest
import com.metest.api.core.http.HttpResponse
import com.metest.api.core.http.HttpResponse.Handler
import com.metest.api.core.http.HttpResponseFor
import com.metest.api.core.http.parseable
import com.metest.api.core.prepareAsync
import com.metest.api.models.rest.actuator.ActuatorBeansParams
import com.metest.api.models.rest.actuator.ActuatorBeansResponse
import com.metest.api.models.rest.actuator.ActuatorConditionsParams
import com.metest.api.models.rest.actuator.ActuatorConditionsResponse
import com.metest.api.models.rest.actuator.ActuatorHealthParams
import com.metest.api.models.rest.actuator.ActuatorHealthResponse
import com.metest.api.models.rest.actuator.ActuatorInfoParams
import com.metest.api.models.rest.actuator.ActuatorInfoResponse
import com.metest.api.models.rest.actuator.ActuatorMappingsParams
import com.metest.api.models.rest.actuator.ActuatorMappingsResponse
import com.metest.api.models.rest.actuator.ActuatorRetrieveParams
import com.metest.api.models.rest.actuator.ActuatorRetrieveResponse
import com.metest.api.models.rest.actuator.ActuatorScheduledtasksParams
import com.metest.api.models.rest.actuator.ActuatorScheduledtasksResponse
import com.metest.api.models.rest.actuator.ActuatorThreaddumpParams
import com.metest.api.models.rest.actuator.ActuatorThreaddumpResponse
import com.metest.api.services.async.rest.actuator.CachServiceAsync
import com.metest.api.services.async.rest.actuator.CachServiceAsyncImpl
import com.metest.api.services.async.rest.actuator.ConfigpropServiceAsync
import com.metest.api.services.async.rest.actuator.ConfigpropServiceAsyncImpl
import com.metest.api.services.async.rest.actuator.EnvServiceAsync
import com.metest.api.services.async.rest.actuator.EnvServiceAsyncImpl
import com.metest.api.services.async.rest.actuator.LoggerServiceAsync
import com.metest.api.services.async.rest.actuator.LoggerServiceAsyncImpl
import com.metest.api.services.async.rest.actuator.MetricServiceAsync
import com.metest.api.services.async.rest.actuator.MetricServiceAsyncImpl
import com.metest.api.services.async.rest.actuator.SbomServiceAsync
import com.metest.api.services.async.rest.actuator.SbomServiceAsyncImpl
import java.util.concurrent.CompletableFuture
import java.util.function.Consumer

class ActuatorServiceAsyncImpl internal constructor(private val clientOptions: ClientOptions) :
    ActuatorServiceAsync {

    private val withRawResponse: ActuatorServiceAsync.WithRawResponse by lazy {
        WithRawResponseImpl(clientOptions)
    }

    private val loggers: LoggerServiceAsync by lazy { LoggerServiceAsyncImpl(clientOptions) }

    private val sbom: SbomServiceAsync by lazy { SbomServiceAsyncImpl(clientOptions) }

    private val metrics: MetricServiceAsync by lazy { MetricServiceAsyncImpl(clientOptions) }

    private val env: EnvServiceAsync by lazy { EnvServiceAsyncImpl(clientOptions) }

    private val configprops: ConfigpropServiceAsync by lazy {
        ConfigpropServiceAsyncImpl(clientOptions)
    }

    private val caches: CachServiceAsync by lazy { CachServiceAsyncImpl(clientOptions) }

    override fun withRawResponse(): ActuatorServiceAsync.WithRawResponse = withRawResponse

    override fun withOptions(modifier: Consumer<ClientOptions.Builder>): ActuatorServiceAsync =
        ActuatorServiceAsyncImpl(clientOptions.toBuilder().apply(modifier::accept).build())

    override fun loggers(): LoggerServiceAsync = loggers

    override fun sbom(): SbomServiceAsync = sbom

    override fun metrics(): MetricServiceAsync = metrics

    override fun env(): EnvServiceAsync = env

    override fun configprops(): ConfigpropServiceAsync = configprops

    override fun caches(): CachServiceAsync = caches

    override fun retrieve(
        params: ActuatorRetrieveParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorRetrieveResponse> =
        // get /rest/actuator
        withRawResponse().retrieve(params, requestOptions).thenApply { it.parse() }

    override fun beans(
        params: ActuatorBeansParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorBeansResponse> =
        // get /rest/actuator/beans
        withRawResponse().beans(params, requestOptions).thenApply { it.parse() }

    override fun conditions(
        params: ActuatorConditionsParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorConditionsResponse> =
        // get /rest/actuator/conditions
        withRawResponse().conditions(params, requestOptions).thenApply { it.parse() }

    override fun health(
        params: ActuatorHealthParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorHealthResponse> =
        // get /rest/actuator/health
        withRawResponse().health(params, requestOptions).thenApply { it.parse() }

    override fun info(
        params: ActuatorInfoParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorInfoResponse> =
        // get /rest/actuator/info
        withRawResponse().info(params, requestOptions).thenApply { it.parse() }

    override fun mappings(
        params: ActuatorMappingsParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorMappingsResponse> =
        // get /rest/actuator/mappings
        withRawResponse().mappings(params, requestOptions).thenApply { it.parse() }

    override fun scheduledtasks(
        params: ActuatorScheduledtasksParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorScheduledtasksResponse> =
        // get /rest/actuator/scheduledtasks
        withRawResponse().scheduledtasks(params, requestOptions).thenApply { it.parse() }

    override fun threaddump(
        params: ActuatorThreaddumpParams,
        requestOptions: RequestOptions,
    ): CompletableFuture<ActuatorThreaddumpResponse> =
        // get /rest/actuator/threaddump
        withRawResponse().threaddump(params, requestOptions).thenApply { it.parse() }

    class WithRawResponseImpl internal constructor(private val clientOptions: ClientOptions) :
        ActuatorServiceAsync.WithRawResponse {

        private val errorHandler: Handler<HttpResponse> =
            errorHandler(errorBodyHandler(clientOptions.jsonMapper))

        private val loggers: LoggerServiceAsync.WithRawResponse by lazy {
            LoggerServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        private val sbom: SbomServiceAsync.WithRawResponse by lazy {
            SbomServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        private val metrics: MetricServiceAsync.WithRawResponse by lazy {
            MetricServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        private val env: EnvServiceAsync.WithRawResponse by lazy {
            EnvServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        private val configprops: ConfigpropServiceAsync.WithRawResponse by lazy {
            ConfigpropServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        private val caches: CachServiceAsync.WithRawResponse by lazy {
            CachServiceAsyncImpl.WithRawResponseImpl(clientOptions)
        }

        override fun withOptions(
            modifier: Consumer<ClientOptions.Builder>
        ): ActuatorServiceAsync.WithRawResponse =
            ActuatorServiceAsyncImpl.WithRawResponseImpl(
                clientOptions.toBuilder().apply(modifier::accept).build()
            )

        override fun loggers(): LoggerServiceAsync.WithRawResponse = loggers

        override fun sbom(): SbomServiceAsync.WithRawResponse = sbom

        override fun metrics(): MetricServiceAsync.WithRawResponse = metrics

        override fun env(): EnvServiceAsync.WithRawResponse = env

        override fun configprops(): ConfigpropServiceAsync.WithRawResponse = configprops

        override fun caches(): CachServiceAsync.WithRawResponse = caches

        private val retrieveHandler: Handler<ActuatorRetrieveResponse> =
            jsonHandler<ActuatorRetrieveResponse>(clientOptions.jsonMapper)

        override fun retrieve(
            params: ActuatorRetrieveParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorRetrieveResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { retrieveHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val beansHandler: Handler<ActuatorBeansResponse> =
            jsonHandler<ActuatorBeansResponse>(clientOptions.jsonMapper)

        override fun beans(
            params: ActuatorBeansParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorBeansResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "beans")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { beansHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val conditionsHandler: Handler<ActuatorConditionsResponse> =
            jsonHandler<ActuatorConditionsResponse>(clientOptions.jsonMapper)

        override fun conditions(
            params: ActuatorConditionsParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorConditionsResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "conditions")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { conditionsHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val healthHandler: Handler<ActuatorHealthResponse> =
            jsonHandler<ActuatorHealthResponse>(clientOptions.jsonMapper)

        override fun health(
            params: ActuatorHealthParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorHealthResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "health")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { healthHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val infoHandler: Handler<ActuatorInfoResponse> =
            jsonHandler<ActuatorInfoResponse>(clientOptions.jsonMapper)

        override fun info(
            params: ActuatorInfoParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorInfoResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "info")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { infoHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val mappingsHandler: Handler<ActuatorMappingsResponse> =
            jsonHandler<ActuatorMappingsResponse>(clientOptions.jsonMapper)

        override fun mappings(
            params: ActuatorMappingsParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorMappingsResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "mappings")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { mappingsHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val scheduledtasksHandler: Handler<ActuatorScheduledtasksResponse> =
            jsonHandler<ActuatorScheduledtasksResponse>(clientOptions.jsonMapper)

        override fun scheduledtasks(
            params: ActuatorScheduledtasksParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorScheduledtasksResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "scheduledtasks")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { scheduledtasksHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }

        private val threaddumpHandler: Handler<ActuatorThreaddumpResponse> =
            jsonHandler<ActuatorThreaddumpResponse>(clientOptions.jsonMapper)

        override fun threaddump(
            params: ActuatorThreaddumpParams,
            requestOptions: RequestOptions,
        ): CompletableFuture<HttpResponseFor<ActuatorThreaddumpResponse>> {
            val request =
                HttpRequest.builder()
                    .method(HttpMethod.GET)
                    .baseUrl(clientOptions.baseUrl())
                    .addPathSegments("rest", "actuator", "threaddump")
                    .build()
                    .prepareAsync(clientOptions, params)
            val requestOptions = requestOptions.applyDefaults(RequestOptions.from(clientOptions))
            return request
                .thenComposeAsync { clientOptions.httpClient.executeAsync(it, requestOptions) }
                .thenApply { response ->
                    errorHandler.handle(response).parseable {
                        response
                            .use { threaddumpHandler.handle(it) }
                            .also {
                                if (requestOptions.responseValidation!!) {
                                    it.validate()
                                }
                            }
                    }
                }
        }
    }
}
